#include "ConfigFileReader.h"

ConfigFileReader::ConfigFileReader(void)
{
}

ConfigFileReader::~ConfigFileReader(void)
{
}
bool ConfigFileReader::loadConfigFromFile(string filePath, configFileDataStruct* configFileData)
{
	ifstream file;
	file.open( filePath.c_str() );

	if( file.is_open() )
	{
		string line;

		while( !file.eof() )
		{
			getline( file, line);
			char* pch = strtok ((char*)line.c_str()," ");
			if(pch != NULL)
			{
				if(!strcmp(pch, "DEFAULT_EYEX") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultEyeX = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_EYEY") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultEyeY = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_EYEZ") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultEyeZ = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_CENTERX") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultCenterX = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_CENTERY") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultCenterY = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_CENTERZ") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultCenterZ = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_UPX") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultUpX = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_UPY") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultUpY = atof(pch);
				}
				else if(!strcmp(pch, "DEFAULT_UPZ") )
				{
					pch = strtok (NULL, " ");
					configFileData->camDefaultUpZ = atof(pch);
				}
				else if(!strcmp(pch, "CAMERA_ROTATION_SPEED") )
				{
					pch = strtok (NULL, " ");
					configFileData->camRotationSpeed = atof(pch);
				}
				else if(!strcmp(pch, "CAMERA_TRANSLATION_SPEED") )
				{
					pch = strtok (NULL, " ");
					configFileData->camTranslationSpeed = atof(pch);
				}
				else if(!strcmp(pch, "TERRAIN_IMAGE_FILE_PATH") )
				{
					pch = strtok (NULL, " ");
					strcpy(configFileData->terrainImageFilePath, pch);
				}
				else if(!strcmp(pch, "TERRAIN_WIDTH") )
				{
					pch = strtok (NULL, " ");
					configFileData->terrainWidth = atoi(pch);
				}
				else if(!strcmp(pch, "TERRAIN_HEIGHT") )
				{
					pch = strtok (NULL, " ");
					configFileData->terrainHeight = atoi(pch);
				}
				else if(!strcmp(pch, "VERTEX_HEIGHT_FACTOR") )
				{
					pch = strtok (NULL, " ");
					configFileData->vertexHeightFactor = atof(pch);
				}
				else if(!strcmp(pch, "HALF_SPHERE_U") )
				{
					pch = strtok (NULL, " ");
					configFileData->nu = atoi(pch);
				}
				else if(!strcmp(pch, "HALF_SPHERE_V") )
				{
					pch = strtok (NULL, " ");
					configFileData->nv = atoi(pch);
				}
				else if(!strcmp(pch, "HALF_SPHERE_TEX_IMAGE") )
				{
					pch = strtok (NULL, " ");
					strcpy(configFileData->sphereTexImage, pch);
				}
				else if(!strcmp(pch, "TERRAIN_TEX_IMAGE") )
				{
					pch = strtok (NULL, " ");
					strcpy(configFileData->terrainTextureImageFilePath, pch);
				}
			}
		}
		return true;
	}
	else
	{
		cerr<< "Cannot open config file "<<filePath<<endl;
		return false;
	}
}
